/* 1.Write a program in C to create and display Singly Linked List. 
    
    Test Data :
    Input the number of nodes : 3
    Input data for node 1 : 5
    Input data for node 2 : 6
    Input data for node 3 : 7

    Expected Output :
    Data entered in the list :                                                                                   
    Data = 5                                                                                                     
    Data = 6                                                                                                     
    Data = 7
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* Nodeptr;
typedef struct node
{
    int x;
    Nodeptr next;    
} Node;

Nodeptr createNodeByNum(int n)
{
    int i;
    Nodeptr root = NULL;
    Nodeptr newNode = NULL;
    Nodeptr curNode = NULL;

    for(i = 0; i < n; i++){
        newNode = (Nodeptr)malloc(sizeof(Node));
        if(newNode == NULL){
            return NULL;
        }
        printf("Input data for node %d : ", i);
        if(root == NULL){                 
            scanf("%d", &(newNode->x));
            newNode->next = NULL;
            root = newNode;
            curNode = newNode;
        }else{           
            scanf("%d", &(newNode->x));
            newNode->next = NULL;
            curNode->next = newNode;
            curNode = newNode;
        }
    }

    return root;
}

void printList(Nodeptr nodeptr)
{
    if(nodeptr == NULL){
        printf("This list is empty!\n");
        return;
    }
    printf("Data entered in the list:\n");
    for(; nodeptr != NULL; nodeptr = nodeptr->next){
        printf("Data = %d\n", nodeptr->x);
    }   
}

int main(int argc, char *argv[])
{
    int n;
    Nodeptr root;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createNodeByNum(n);
    printList(root);

    return 0;
}